package com.ttj.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ttj.domain.oracle.OrderInstock;
import com.ttj.domain.oracle.OrderInstockDetail;
import com.ttj.mapper.oracle.OrderInstockDetailMapper;
import com.ttj.mapper.oracle.OrderInstockMapper;
import com.ttj.service.GoDownKeeperService;
import com.ttj.util.InitTimeUtil;
import com.ttj.vo.BaseRes;
@Service
public class GoDownKeeperServiceImpl implements GoDownKeeperService {

	@Resource
	private OrderInstockMapper mOrderInstockMapper;
	@Resource
	private OrderInstockDetailMapper mOrderInstockDetailMapper;
	@Override
	/**
	 * 展示服务组和网点未验收数量
	 */
	public BaseRes<OrderInstock> getUncheckedCount() {
		// TODO Auto-generated method stub
		BaseRes<OrderInstock> brs = new BaseRes<OrderInstock>();
		int purchaserCount = mOrderInstockMapper.getPurchaserCount();
		int serviceCount = mOrderInstockMapper.getServiceCount();
		OrderInstock one = new OrderInstock();
		one.setPurchaserCount(purchaserCount);
		one.setServiceCount(serviceCount);
		brs.setData(one);
		return brs;
	}
	@Override
	/**
	 * 展示收购员未验收、已验收、挂起列表
	 */
	public BaseRes<Map> showPurchaserList() {
		// TODO Auto-generated method stub
		BaseRes<Map> brs = new BaseRes<Map>();
		Map<String, Object> map = new HashMap<String, Object>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		Calendar cal = Calendar.getInstance(); 
		cal.add(Calendar.MONTH, -1);
		String valdateDate = InitTimeUtil.getStartDaytime(cal);
		List<OrderInstock> getDrivers = mOrderInstockMapper.getPurchaserDriversList();
		List<OrderInstock> uncheckedList = mOrderInstockMapper.getPurchaserUncheckedList();
		List<OrderInstock> checkedList = mOrderInstockMapper.getPurchaserCheckedList(valdateDate);
		List<OrderInstock> hangingList = mOrderInstockMapper.getPurchaserHangingList();
		for(OrderInstock cc:checkedList){
			cc.setValiDate(sdf.format(cc.getValdateDate()));
		}
		for(OrderInstock cc:hangingList){
			cc.setHangingDate(sdf.format(cc.getHangDate()));
		}
		map.put("getDrivers", getDrivers);//网点未验收列表中的司机和车牌
		map.put("uncheckedList", uncheckedList);//网点未验收列表中的单号和件数重量
		map.put("checkedList", checkedList);//网点已验收列表中的数据
		map.put("hangingList", hangingList);//网点挂起列表中的数据
		brs.setData(map);
		return brs;
	}
	@Override
	/**
	 * 展示收购员订单详情
	 */
	public BaseRes<OrderInstock> showPurchaserDetail(String orderId) {
		// TODO Auto-generated method stub
		BaseRes<OrderInstock> brs = new BaseRes<OrderInstock>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		List<OrderInstockDetail> getDetail= mOrderInstockDetailMapper.getOneByOrderId(orderId);
		for(OrderInstockDetail gd:getDetail){
			if(gd.getUnit().equals("1")){
				gd.setUnit("个");
			}else{
				gd.setUnit("公斤");
			}
		}
		List<OrderInstock> getOneById = mOrderInstockMapper.getPurchaserDetailByorderId(orderId);
		OrderInstock details = getOneById.get(0);
		details.setDeliveryDateStr(sdf.format(details.getDeliveryDate()));
		if(details.getValdateDate()!=null){
			details.setValiDate(sdf.format(details.getValdateDate()));
		}
		if(details.getHangDate()!=null){
			details.setHangingDate(sdf.format(details.getHangDate()));
		}
		details.setOrderListDetail(getDetail);
		brs.setData(details);
		return brs;
	}
	@Override
	/**
	 * 未验收订单的验收功能
	 */
	public BaseRes<OrderInstock> checkOrderInstock(String orderId,List<OrderInstockDetail> fromJson) {
		// TODO Auto-generated method stub
		BaseRes<OrderInstock> brs = new BaseRes<OrderInstock>();
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> map1 = new HashMap<String, Object>();
		Date valdateDate = new Date();
		map.put("valdateDate", valdateDate);
		map.put("orderId", orderId);
		int result = mOrderInstockMapper.checkOrderInstock(map);
		for(OrderInstockDetail fj:fromJson){
			map1.put("orderId", fj.getOrderId());
			map1.put("gId", fj.getgId());
			map1.put("realNum", fj.getNum());
			map1.put("realWeight", fj.getWeight());
			mOrderInstockDetailMapper.updateDetails(map1);
		}
		if(result == 1){
			brs.setMsg("验收成功");
		}else{
			brs.setMsg("验收失败");
		}
		return brs;
	}
	@Override
	/**
	 * 未验收订单的挂起功能
	 */
	public BaseRes<OrderInstock> hangingOrderInstock(String orderId) {
		// TODO Auto-generated method stub
		BaseRes<OrderInstock> brs = new BaseRes<OrderInstock>();
		Map<String, Object> map = new HashMap<String, Object>();
		Date hangingDate = new Date();
		map.put("hangingDate", hangingDate);
		map.put("orderId", orderId);
		int result = mOrderInstockMapper.hangingOrderInstock(map);
		if(result == 1){
			brs.setMsg("挂起成功");
		}else{
			brs.setMsg("挂起失败");
		}
		return brs;
	}
	@Override
	/**
	 * 展示服务组未验收、已验收、挂起列表
	 */
	public BaseRes<Map> showServiceList() {
		// TODO Auto-generated method stub
		BaseRes<Map> brs = new BaseRes<Map>();
		Map<String, Object> map = new HashMap<String, Object>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		Calendar cal = Calendar.getInstance(); 
		cal.add(Calendar.MONTH, -1);
		String valdateDate = InitTimeUtil.getStartDaytime(cal);
		List<OrderInstock> uncheckedList = mOrderInstockMapper.getServiceUncheckedList();
		List<OrderInstock> checkedList = mOrderInstockMapper.getServiceCheckedList(valdateDate);
		List<OrderInstock> hangingList = mOrderInstockMapper.getServiceHangingList();
		for(OrderInstock cc:uncheckedList){
			cc.setDeliveryDateStr(sdf.format(cc.getDeliveryDate()));
		}
		for(OrderInstock cc:checkedList){
			cc.setValiDate(sdf.format(cc.getValdateDate()));
		}
		for(OrderInstock cc:hangingList){
			cc.setHangingDate(sdf.format(cc.getHangDate()));
		}
		map.put("uncheckedList", uncheckedList);//服务组未验收列表中的单号和件数重量
		map.put("checkedList", checkedList);//服务组已验收列表中的数据
		map.put("hangingList", hangingList);//服务组挂起列表中的数据
		brs.setData(map);
		return brs;
	}

	@Override
	/**
	 * 展示收购员订单详情
	 */
	public BaseRes<OrderInstock> showServiceDetail(String orderId) {
		// TODO Auto-generated method stub
		BaseRes<OrderInstock> brs = new BaseRes<OrderInstock>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		List<OrderInstockDetail> getDetail= mOrderInstockDetailMapper.getOneByOrderId(orderId);
		for(OrderInstockDetail gd:getDetail){
			if(gd.getUnit().equals("1")){
				gd.setUnit("个");
			}else{
				gd.setUnit("公斤");
			}
		}
		List<OrderInstock> getOneById = mOrderInstockMapper.getServiceDetailByorderId(orderId);
		OrderInstock details = getOneById.get(0);
		details.setDeliveryDateStr(sdf.format(details.getDeliveryDate()));
		if(details.getValdateDate()!=null){
			details.setValiDate(sdf.format(details.getValdateDate()));
		}
		if(details.getHangDate()!=null){
			details.setHangingDate(sdf.format(details.getHangDate()));
		}
		details.setOrderListDetail(getDetail);
		brs.setData(details);
		return brs;
	}
	
	@Override
	/**
	 * 用于展示库管工作日志（提取条件部分）
	 */
	public BaseRes<Map> showKeeperDailyLog(String searchTimeType,String workerType, String startDate, String endDate) {
		BaseRes<Map> brs = new BaseRes<Map>();
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> condition = new HashMap<String, Object>();
		System.out.println("searchTimeType:"+searchTimeType+"  workerType:"+workerType+"  startDate:"+startDate+"  endDate:"+endDate);
		condition.put("workerType", workerType);
		if(startDate==null){
			System.out.println("empty");
		}else if(startDate.trim().equals("")){
			System.out.println("another empty");
		}else{
			System.out.println("not empty");
		}
		if((!startDate.trim().equals(""))&&(!endDate.trim().equals(""))){//按照时间搜索
			condition.put("startTime", startDate);
			condition.put("endTime", endDate);
		}else{
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			if(searchTimeType.equals("1")){//搜索当日的数据
				Calendar cal = Calendar.getInstance(); 
				String endTime = sdf.format(cal.getTime());
				String startTime = InitTimeUtil.getStartDaytime(cal);
				condition.put("startTime", startTime);
				condition.put("endTime", endTime);
			}else if(searchTimeType.equals("2")){//搜索当周的数据
				Calendar cal = Calendar.getInstance();
				String endTime = sdf.format(cal.getTime());
				cal.add(Calendar.DATE, cal.getFirstDayOfWeek()-cal.get(Calendar.DAY_OF_WEEK)+1);
				String startTime = InitTimeUtil.getStartDaytime(cal);
				condition.put("startTime", startTime);
				condition.put("endTime", endTime);
				
			}else if(searchTimeType.equals("3")){//搜索当月的数据
				Calendar cal = Calendar.getInstance();
				String endTime = sdf.format(cal.getTime());
				cal.set(Calendar.DATE, 1);
				String startTime = InitTimeUtil.getStartDaytime(cal);
				condition.put("startTime", startTime);
				condition.put("endTime", endTime);
				
			}else if(searchTimeType.equals("4")){//搜索昨日的数据
				Calendar cal = Calendar.getInstance(); 
				String endTime = InitTimeUtil.getStartDaytime(cal);
				cal.add(Calendar.DATE, -1);
				String startTime = InitTimeUtil.getStartDaytime(cal);
				condition.put("startTime", startTime);
				condition.put("endTime", endTime);
			}else if(searchTimeType.equals("5")){//搜索上周的数据
				Calendar cal = Calendar.getInstance();
				cal.add(Calendar.DATE, -7);
				cal.add(Calendar.DATE, cal.getFirstDayOfWeek()-cal.get(Calendar.DAY_OF_WEEK)+1);
				String startTime = InitTimeUtil.getStartDaytime(cal);
				cal.add(Calendar.DATE, 7);
				String endTime = InitTimeUtil.getStartDaytime(cal);
				condition.put("startTime", startTime);
				condition.put("endTime", endTime);
			}else{//搜索上个月的数据
				Calendar cal = Calendar.getInstance();
				cal.add(Calendar.MONTH, -1);
				cal.set(Calendar.DATE, 1);
				String startTime = InitTimeUtil.getStartDaytime(cal);
				cal.add(Calendar.MONTH, 1);
				String endTime = InitTimeUtil.getStartDaytime(cal);
				condition.put("startTime", startTime);
				condition.put("endTime", endTime);
				System.out.println(startTime+"%%%"+endTime);
			}
		}
		map = showKeeperDailyLogByTime(condition);
		brs.setData(map);
		return brs;
	}
	@Override
	/**
	 * 用于展示库管工作日志（数据库查询部分）
	 */
	public Map<String, Object> showKeeperDailyLogByTime(Map<String, Object> condition) {
		// TODO Auto-generated method stub
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Map<String, Object> map = new HashMap<String, Object>();
		System.out.println(condition);
//		int acceptTimes = mOrderInstockMapper.getAcceptTimes(condition);
		int acceptTimes = 0;
		int hangingTimes = mOrderInstockMapper.getHangingTimes(condition);
		List<OrderInstock> acceptList = mOrderInstockMapper.getAcceptList(condition);
		List<OrderInstock> delayList = new ArrayList<OrderInstock>();
		int delayNum = 0;
		for(OrderInstock ac:acceptList){
			if(!sdf.format(ac.getDeliveryDate()).equals(sdf.format(ac.getValdateDate()))){
				delayNum++;
				ac.setValiDate(sdf.format(ac.getValdateDate()));
				delayList.add(ac);
			}
		}
		List<OrderInstock> hangingList = mOrderInstockMapper.getHangingList(condition);
		for(OrderInstock hh:hangingList){
			System.out.println(hh.toString());
		}
		List<OrderInstockDetail> countAcceptedDetails = mOrderInstockDetailMapper.countAcceptedDetails(condition);
		List<OrderInstockDetail> showCountAcceptedDetails = new ArrayList<OrderInstockDetail>();
		for(OrderInstockDetail cad:countAcceptedDetails){
			if(cad.getTotalWeight()!=0){
				if(cad.getUnit().equals("1")){
					cad.setUnit("个");
				}else{
					cad.setUnit("公斤");
				}
				showCountAcceptedDetails.add(cad);
			}
		}
		List<OrderInstockDetail> countHangingDetails = mOrderInstockDetailMapper.countHangingDetails(condition);
		List<OrderInstockDetail> showCountHangingDetails = new ArrayList<OrderInstockDetail>();
		for(OrderInstockDetail cad:countHangingDetails){
			if(cad.getTotalWeight()!=0){
				if(cad.getUnit().equals("1")){
					cad.setUnit("个");
				}else{
					cad.setUnit("公斤");
				}
				showCountHangingDetails.add(cad);
			}
		}
		map.put("acceptTimes", acceptTimes);//工作日志验收次数
		map.put("hangingTimes", hangingTimes);//工作日志挂起次数
		map.put("delayNum", delayNum);//延迟验货次数
		map.put("delayList", delayList);//延迟验货列表
		map.put("hangingList", hangingList);//挂起订单列表
		map.put("showCountAcceptedDetails", showCountAcceptedDetails);//工作日志验收量的统计
		map.put("showCountHangingDetails", showCountHangingDetails);//工作日志挂起量的统计
		return map;
	}
	
}
